package fhp.hlhj.giantfold.fragment
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.GainDetailAty
import fhp.hlhj.giantfold.activity.annualized.ShowAnnualAty
import fhp.hlhj.giantfold.activity.login.WechatLoginAty
import fhp.hlhj.giantfold.activity.malls.JdOrderListAty
import fhp.hlhj.giantfold.activity.malls.TBOrderListAty
import fhp.hlhj.giantfold.activity.message.MessageAty
import fhp.hlhj.giantfold.activity.orders.OrderDetail
import fhp.hlhj.giantfold.activity.red.ShakeRedAty
import fhp.hlhj.giantfold.activity.sharejz.ShareAppAty
import fhp.hlhj.giantfold.activity.sharejz.SharePostAty
import fhp.hlhj.giantfold.activity.user.BindPhoneAty
import fhp.hlhj.giantfold.activity.user.HelpAty
import fhp.hlhj.giantfold.activity.user.OrderAty
import fhp.hlhj.giantfold.activity.user.UserCenter
import fhp.hlhj.giantfold.customView.MyHeader
import fhp.hlhj.giantfold.interfaces.IAdv
import fhp.hlhj.giantfold.interfaces.ILatestOrder
import fhp.hlhj.giantfold.interfaces.IUser
import fhp.hlhj.giantfold.javaBean.AdvBean
import fhp.hlhj.giantfold.javaBean.LatestOrderBean
import fhp.hlhj.giantfold.javaBean.UserInfoBean
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.presenter.AdvPresenter
import fhp.hlhj.giantfold.presenter.LatestOrderPresenter
import fhp.hlhj.giantfold.presenter.MessageCenterPresenter
import fhp.hlhj.giantfold.presenter.UserPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import fhp.hlhj.giantfold.utils.SPUtils
import kotlinx.android.synthetic.main.fgm_growmoeny.*
import kotlinx.android.synthetic.main.fgm_self_logined.*
import kotlinx.android.synthetic.main.fgm_self_logined.view.*
import java.text.DecimalFormat
import java.util.ArrayList

/**
 * author: FHP
 * data: on 2017\11\18 0018 10:08
 * E-Mail: 103945089@qq.com
 */
class SelfLoginedFgm : Fragment(),IUser, ILatestOrder, IAdv, SpringView.OnFreshListener {

    private  lateinit var rootView:View
    private lateinit var userPresenter:UserPresenter
    private var oid=""
    private lateinit var advPresenter: AdvPresenter
    private lateinit var latestoderPresenter:LatestOrderPresenter
    public fun reSet(){
        val httpParams = HttpParams()
        httpParams.put("block",2)
        advPresenter.getAdv(httpParams)

        //最新订单
        val params = HttpParams()
        params.put("user_id",Contents.userId)
        latestoderPresenter.getLatestOder(params)
//        用户信息
        val p = HttpParams()
        p.put("user_id",Contents.userId)
        userPresenter.getUser(p)

    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView = inflater.inflate(R.layout.fgm_self_logined, null)
        userPresenter= UserPresenter(this)
        latestoderPresenter= LatestOrderPresenter(this)
        advPresenter= AdvPresenter(this)
        /*广告信息*/
        val httpParams = HttpParams()
        httpParams.put("block",2)
        advPresenter.getAdv(httpParams)

        /*用户信息*/
        val p = HttpParams()
        p.put("user_id",Contents.userId)
        userPresenter.getUser(p)
        //最新订单
        val params = HttpParams()
        params.put("user_id",Contents.userId)
        latestoderPresenter.getLatestOder(params)
            initListner()
        rootView.spView.header= MyHeader()
        rootView.spView.callFresh()
        rootView.spView.setListener(this)
        /*设置横向菜单*/
        val strs=ArrayList<String>()
        strs.add("1")
        strs.add("1")
        strs.add("1")
        val adp  = object :BaseQuickAdapter<String,BaseViewHolder>(R.layout.caidan_item,strs){
            override fun convert(helper: BaseViewHolder?, item: String?) {
                when(helper?.layoutPosition){
                    0->{
                        helper?.setBackgroundRes(R.id.lo,R.drawable.share_1)
                        helper?.setText(R.id.tv1,"分享巨折")
                        helper?.setText(R.id.tv2,"邀请好友涨收益")
                        helper?.setTextColor(R.id.tv1,resources.getColor(R.color.share_1))
                        helper?.setTextColor(R.id.tv2,resources.getColor(R.color.share_1))
                    }
                    1->{
                        helper?.setBackgroundRes(R.id.lo,R.drawable.share_2)
                        helper?.setText(R.id.tv1,"分享海报")
                        helper?.setText(R.id.tv2,"越分享越赚钱")
                        helper?.setTextColor(R.id.tv1,resources.getColor(R.color.share_2))
                        helper?.setTextColor(R.id.tv2,resources.getColor(R.color.share_2))
                    }
                    2->{
                        helper?.setBackgroundRes(R.id.lo,R.drawable.share_3)
                        helper?.setText(R.id.tv1,"好友助力")
                        helper?.setText(R.id.tv2,"好友帮忙加油")
                        helper?.setTextColor(R.id.tv1,resources.getColor(R.color.share_3))
                        helper?.setTextColor(R.id.tv2,resources.getColor(R.color.share_3))

                    }
                }
            }
        }

        rootView.shareRecycler.adapter=adp
        rootView.shareRecycler.layoutManager=LinearLayoutManager(context,LinearLayout.HORIZONTAL,false)
        adp.setOnItemClickListener { adapter, view, position ->
        when(position){
                    0->{
                        context.startActivity(Intent(context, ShareAppAty::class.java))
                    }
            1->{
                context.startActivity(Intent(context, SharePostAty::class.java))

            }
            2->{
                context.startActivity(Intent(context, ShowAnnualAty::class.java))

            }
        }

        }
        setData()
        return rootView
    }

    private fun initListner() {
        rootView.btMessage.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{
                context.startActivity(Intent(context,MessageAty::class.java))
            }
        }
        rootView.btOrder.setOnClickListener {
            //            context.startActivity(Intent(context, TBOrderListAty::class.java))
        }
        rootView.btSelfOrder.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{

                context.startActivity(Intent(context,OrderAty::class.java))
            }
        }
        rootView.btHelp.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{

                context.startActivity(Intent(context,HelpAty::class.java))
            }
            //            context.startActivity(Intent(context,JdOrderListAty::class.java))

        }
        rootView.btShare.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{
                context.startActivity(Intent(context,ShareAppAty::class.java))

            }
        }
        rootView.btDetail.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{
                context.startActivity(Intent(context,GainDetailAty::class.java))

            }
        }
        rootView.btRed.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{
                context.startActivity(Intent(context,ShakeRedAty::class.java))

            }
        }
        rootView.loLatest.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{
                val intent = Intent(context, OrderDetail::class.java)
                intent.putExtra("oid",oid)
                context.startActivity(intent)
            }

        }
        rootView.btMore.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{
                val intent = Intent(context, OrderAty::class.java)
                context.startActivity(intent)
            }
        }

        rootView.btSelf.setOnClickListener {
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
                Glide.with(context).load(R.mipmap.placeholdericon).asBitmap().dontAnimate().into(rootView.btSelf)
            }else{
                val intent = Intent(context, UserCenter::class.java)
                activity.startActivityForResult(intent,2)
            }
        }
    }

    private fun setData() {

    }

    override fun onResume() {
        super.onResume()
        LogUtil.log("我OnResume")
        //最新订单
        val params = HttpParams()
        params.put("user_id",Contents.userId)
        latestoderPresenter.getLatestOder(params)
//        用户信息
        val p = HttpParams()
        p.put("user_id",Contents.userId)
        userPresenter.getUser(p)
    }
    /*override fun onResume() {
        super.onResume()
        *//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
         * 根据登录类型判断
         *//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//*
        LogUtil.logi("登录类型${SPUtils.readLoginType(context)}userId=${Contents.userId}")
        when(SPUtils.readLoginType(context)){
            -1->{
                rootView.btSelf.setOnClickListener{

                    context.startActivity(Intent(context, WechatLoginAty::class.java))
                }
                Glide.with(context).load(R.mipmap.exp5).asBitmap().dontAnimate().into(rootView.btSelf)
            }
            else->{
                if (Contents.userId.equals("0")){
                    rootView.btSelf.setOnClickListener{
                        context.startActivity(Intent(context, BindPhoneAty::class.java))
                    }
                }else{
                    val params = HttpParams()
                    params.put("user_id",Contents.userId)
                    latestoderPresenter.getLatestOder(params)
                    val httpParams = HttpParams()
                    httpParams.put("user_id",Contents.userId)
                    userPresenter.getUser(httpParams)
                    rootView.btSelf.setOnClickListener{
                        context.startActivity(Intent(context, UserCenter::class.java))
                    } //最新订单

                }
            }
        }
    }*/


    override fun showLoading() {

    }

    override fun getUser(userInfoBean: UserInfoBean) {
        rootView.spView.onFinishFreshAndLoad()

        if (userInfoBean.code==200&&context!=null){
            Glide.with(context).load(Urls.BaseUrl+"/"+userInfoBean.data.head_pic).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.btSelf)
            val userInfo = userInfoBean.data
            val df = DecimalFormat("0.000")
            if (!Contents.userId.equals("0")){
                try {
                    rootView.totalNum.text= df.format(userInfo?.earnings_accumulated?.toDouble()).toString()
                    rootView.tvTodayGain.text=df.format(userInfo?.earnings_today?.toDouble()).toString()
                    rootView.tvSpendAll.text=df.format(userInfo?.principal?.toDouble()).toString()
                    rootView.tvAvailable.text=df.format(userInfo?.balance?.toDouble()).toString()
                }catch (e:Exception){

                }
            }
        }
    }

    override fun getLastestOrder(latestOrderBean: LatestOrderBean) {
        rootView.spView.onFinishFreshAndLoad()
        try {
            if (latestOrderBean.code==200){
                if (latestOrderBean.data!=null){
                    loLatest.visibility=View.VISIBLE
                    if (context!=null){
                        if (latestOrderBean.data.tkjd_sp.equals("1")){
                            Glide.with(context).load(latestOrderBean.data.product_pic_url).asBitmap().dontAnimate().placeholder(R.mipmap.placeholdericon).into(rootView.latestImg)
                        }else{
                            if (latestOrderBean.data.product_name.equals("新人红包")){
                                Glide.with(context).load(R.mipmap.nianhuashouyi_hb).asBitmap().dontAnimate().placeholder(R.mipmap.exp5).into(rootView.latestImg)
                            }else{
                                Glide.with(context).load(latestOrderBean.data.pict_url).asBitmap().dontAnimate().placeholder(R.mipmap.placeholdericon).into(rootView.latestImg)

                            }
                        }
                    }
                    rootView.latestTitle.text=latestOrderBean.data.product_name
                    rootView.latestTime.text=latestOrderBean.data.updated_time
                    oid=latestOrderBean.data.id
                }else{
                    loLatest.visibility=View.GONE
                }
                rootView.latestStatus.text=when(latestOrderBean.data.deposit_status){
                    "0"->{
                        "未存入"
                    }
                    "1"->{
                        "已存入"
                    }
                    "2"->{
                        "不存入"
                    }
                    else->{
                        ""
                    }
                }
            }
        }catch (e:Exception){

        }

    }
// 获取广告位
    override fun getADV(advBean: AdvBean) {
    rootView.spView.onFinishFreshAndLoad()
    if (advBean.code==200){
            Glide.with(context).load(Urls.BaseUrl+advBean.data[0].adv_img).dontAnimate().into(rootView.hotImg)
        when(advBean.data[0].jump_type.equals("1")){
            
        }
    }
    }

    override fun onLoadmore() {
    }

    override fun onRefresh() {
        //最新订单
        val params = HttpParams()
        params.put("user_id",Contents.userId)
        latestoderPresenter.getLatestOder(params)

        /*广告*/
        val httpParams = HttpParams()
        httpParams.put("block",2)
        advPresenter.getAdv(httpParams)
        /*用户信息*/
        val userParams = HttpParams()
        userParams.put("user_id",Contents.userId)
        userPresenter.getUser(httpParams)

    }
}